package vectorspacemodel;
import javax.swing.*;
import java.awt.event.*;
//import java.awt.*;

public class SearchEngine extends JFrame implements ActionListener
{
 JButton search,create_index;
 DefaultListModel list_model;
 JList relevant_documents;
 JTextField search_box;
 //Image img;
 SearchEngine()
 {
  this.setVisible(true);
  this.setSize(800,600);
  this.setTitle("Search Engine");
  this.setLayout(null);
  this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 }
 
 public void arrange()
 {
  /*ImageIcon image=new ImageIcon("C:/Program Files/Java/skull.GIF");
  img=image.getImage();
  repaint();*/
     
  list_model = new DefaultListModel();
  relevant_documents = new JList(list_model);
  relevant_documents.setSelectionMode(
  ListSelectionModel.SINGLE_SELECTION);
  relevant_documents.setVisibleRowCount(10);
  JScrollPane scrollPane = new JScrollPane(relevant_documents);
  this.add(scrollPane);
  		
  search=new JButton("Search");
  create_index=new JButton("CreateIndex");
  search.setEnabled(false);
  search_box=new JTextField();
  
  this.add(search);
  this.add(create_index);
  this.add(relevant_documents);
  this.add(search_box);
  
  search.addActionListener(this);
  create_index.addActionListener(this);
  //relevant_documents.addListSelectionListener(this);
  relevant_documents.addMouseListener(new MouseAdapter() 
  {
   public void mouseClicked(MouseEvent evt) 
   {
    JList list = (JList) evt.getSource();
    if (evt.getClickCount() == 2) 
        { // Double-click
        int index = list.locationToIndex(evt.getPoint());
        String document=(String)list.getSelectedValue();
        System.out.println("selected value is  "+document);
        try
            {
            (Runtime.getRuntime()).exec ("notepad "+document); 
            }
        catch(Exception ee)
            {
            System.out.println("Exception during opening the file "+ee);
            }
        } 
    }
  });
  
  search_box.setBounds(280,100,300,30);
  search.setBounds(320,150,100,30);
  create_index.setBounds(440,150,105,30);
  relevant_documents.setBounds(240,230,400,300);
  
  
 }
 
 /*public void paint( Graphics g ) 
 {
  g.drawImage( img, 0, 0,this );
 }*/
 public void actionPerformed(ActionEvent ae)
 {
  String button_name=ae.getActionCommand();
  if(button_name.equals("CreateIndex"))
    {
        try
        {
        Indexing ii=new Indexing();
        ii.get_Documents_From_Directory("C:/Program Files/Java/Documents");
        ii.create_index();
        ii.create_index_obj(); 
        }
        catch(Exception e)
        {
         System.out.println("Exception during indexing"+e);
        }
    search.setEnabled(true);
    //create_index.setEnabled(false);
    }
  else
    {
     String query=search_box.getText();
     if(query.isEmpty())
        {
         System.out.println("please enter a query");
        }
     else
        {
         list_model.clear();
         int result[];
         result=null;
         VectorSpaceModel vsm=new VectorSpaceModel();
         //System.out.println("enetered query is"+query); 
         try
            {
            
            vsm.read_index_obj();
            vsm.get_Query_Doc(query);
            vsm.get_Documents_From_Directory();
            vsm.cal_Idf();
            vsm.cal_Tf_Idf_Query();
            vsm.cal_Tf_Idf_Doc();
            //vsm.display_Sc(); 
            result=vsm.get_result();
            }
         catch(Exception e)
            {
            System.out.println("Exception during vsm"+e);
            }
         if(result.length==0)
            {
             list_model.addElement("NO Documents Found related to the given query");
            }
         else
            {
             for(int i=0;i<result.length;i++)
                {
                list_model.addElement(vsm.doc_paths.get(result[i]));
                }
            }
         
        }
    }
 }
 
 public static void main(String[] args) 
 {
  SearchEngine se=new SearchEngine();
  se.arrange();
 }
}

